package router

import (
	"shop-manager/internal/logic"
	"shop-manager/internal/repos/goods"
	"shop-manager/internal/vo"

	"github.com/gin-gonic/gin"
)

// 商品管理
func RegisterGoods(api *gin.RouterGroup,
	goodsRepo *goods.GoodsRepo,
	goodsLogic *logic.GoodsLogic,
	handleBizRsp func(ctx *gin.Context, rsp any, err error),
	handleBizListRsp func(ctx *gin.Context, rsp any, total int64, err error),
) {
	goodsApiGroup := api.Group("/goods")
	{
		goodsApiGroup.POST("/create", func(ctx *gin.Context) {
			var req vo.CreateGoodsReq
			if err := ctx.BindJSON(&req); err != nil {
				return
			}

			rsp, err := goodsLogic.CreateGoods(ctx, req)
			handleBizRsp(ctx, rsp, err)
		})

		goodsApiGroup.POST("/update", func(ctx *gin.Context) {
			var req vo.UpdateGoodsReq
			if err := ctx.BindJSON(&req); err != nil {
				return
			}
			rsp, err := goodsLogic.UpdateGoods(ctx, req)
			handleBizRsp(ctx, rsp, err)
		})

		goodsApiGroup.POST("/delete", func(ctx *gin.Context) {
		})

		goodsApiGroup.POST("/list", func(ctx *gin.Context) {
			var req vo.ListGoodsReq
			if err := ctx.BindJSON(&req); err != nil {
				return
			}
			listRsp, err := goodsLogic.ListGoods(ctx, req)
			handleBizRsp(ctx, listRsp, err)
		})

		goodsApiGroup.POST("/import", func(ctx *gin.Context) {
			var req vo.ImportGoodsReq
			if err := ctx.BindJSON(&req); err != nil {
				return
			}

			rsp, err := goodsLogic.ImportGoods(ctx, req)
			handleBizRsp(ctx, rsp, err)
		})

		// 批量更新商品
		goodsApiGroup.POST("/batch_update", func(ctx *gin.Context) {
			var req vo.BatchUpdateReq
			if err := ctx.BindJSON(&req); err != nil {
				return
			}

			rsp, err := goodsLogic.BatchUpdate(ctx, req)
			handleBizRsp(ctx, rsp, err)
		})
	}
}
